package com.jzo2o.customer.controller.consumer;

import com.jzo2o.common.model.PageResult;
import com.jzo2o.common.model.dto.PageQueryDTO;
import com.jzo2o.customer.model.domain.AddressBook;
import com.jzo2o.customer.model.dto.request.AddressBookPageQueryReqDTO;
import com.jzo2o.customer.model.dto.request.AddressBookUpsertReqDTO;
import com.jzo2o.customer.service.ConsumerAddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 普通用户相关接口
 *
 * @author itcast
 * @create 2023/7/7 19:34
 **/
@RestController("consumerAddressController")
@RequestMapping("/consumer")
@Api(tags = "用户端 - 普通用户相关接口")
@Slf4j
public class ConsumerAddressController {

    @Resource
    private ConsumerAddressService consumerAddressService;

    @GetMapping("address-book/page")
    @ApiOperation("分页查询地址列表")
    public PageResult<AddressBook> findByPage(AddressBookPageQueryReqDTO dto) {
        log.info("获取到的数据为{}", dto);
        return consumerAddressService.findByPage(dto);
    }


    @PostMapping("address-book")
    @ApiOperation("新增地址")
    public void add(@RequestBody AddressBookUpsertReqDTO dto) {
         consumerAddressService.add(dto);
    }


    @GetMapping("address-book/{id}")
    @ApiOperation("通过id地址")
    public AddressBook findById(@PathVariable(value = "id") Long addressBookId) {
       return consumerAddressService.findById(addressBookId);
    }

    @PutMapping("address-book/default")
    @ApiOperation("修改默认地址")
    public void defaultAddress(@RequestParam(value = "id")Long addressBookId,
                                      @RequestParam (value = "flag")Integer flag) {
        consumerAddressService.setDefaultAddress(addressBookId,flag);
    }

    @PutMapping("address-book/{id}")
    @ApiOperation("编辑地址信息")
    public void updateAddress(@PathVariable(value = "id") Long addressBookId,
                                     @RequestBody AddressBookUpsertReqDTO dto) {
         consumerAddressService.update(addressBookId,dto);
    }

    @DeleteMapping("address-book/batch")
    @ApiOperation("批量删除地址信息")
    public void batchDeleteAddress(@RequestBody List<Long> ids) {
        consumerAddressService.batchDelete(ids);
    }

}
